import 'package:flutter/material.dart';

import 'package:image_picker/image_picker.dart';
import 'package:imember_printer/imember_printer.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  /* ----------iOS測試開始----------- */
    void _connectPrinter() {
      ImemberPrinter.connectPrinter();
    }

    void _printInventoryBill() {
      final products = [
        Product(name: '百年孤獨', number: '5'),
        Product(name: '少有人走的路', number: '100'),
        Product(name: '追風箏的人', number: '523'),
        Product(name: '紅樓夢', number: '24235'),
        Product(name: '一只特立獨行的豬', number: '524234'),
        Product(name: '白夜行', number: '522'),
        Product(name: '人類簡史三部曲(人類簡史、未來簡史、今日簡史)', number: '53534'),
        Product(name: '老人與海', number: '1'),
      ];
      ImemberPrinter.printInventoryBill(products);
  }

  void _printInventoryAdjustment() {
    final products = [
      Product(name: '百年孤獨', differenceNumber: '5', differenceReason: '過期報廢'),
      Product(name: '少有人走的路', differenceNumber: '100'),
      Product(name: '追風箏的人', differenceNumber: '523'),
      Product(name: '紅樓夢', differenceNumber: '24235'),
      Product(name: '一只特立獨行的豬', differenceNumber: '524234', differenceReason: '商品不見'),
      Product(name: '人類簡史三部曲(人類簡史、未來簡史、今日簡史)', differenceNumber: '53534', differenceReason: '山有木兮木有枝，心悅君兮君不如。人生若只如初見，何事秋風悲畫扇。'),
    ];
    ImemberPrinter.printInventoryAdjustment(products);
  }

  void _printCheckoutBill() {
    ImemberPrinter.printCheckoutBill();
  }

  void _printTransactionDetails() {
    final products = [
      Product(name: '百年孤獨', number: '5', price: "120"),
      Product(name: '少有人走的路', number: '100', price: "80"),
      Product(name: '追風箏的人', number: '523', price: "76"),
      Product(name: '紅樓夢', number: '2425',price: "2"),
      Product(name: '一只特立獨行的豬', number: '54', price: "20"),
      Product(name: '白夜行', number: '522', price: "23"),
      Product(name: '人類簡史三部曲(人類簡史、未來簡史、今日簡史)', number: '54', price: "123"),
      Product(name: '老人與海', number: '1', price: "145"),
    ];
    ImemberPrinter.printTransactionDetails(products);
  }

  void _printShippingLabel() {
    ImemberPrinter.printShippingLabel();
  }
  /* ----------iOS測試結束----------- */


  /* ----------Android測試開始----------- */
  String content = """
  繁體字繁體字繁體字繁體字
  繁體字繁體字繁體字
  繁體字繁體字繁體字
  繁體字繁體字繁體字
  """;
  final ImagePicker _picker = ImagePicker();
  printImage() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image?.name != null) {
      TP809.printImage(filePath: image?.path ?? "", size: 576).catchError((e) {
        print(e.toString());
      });
    }
  }
  /* ----------Android測試結束----------- */

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: SizedBox(
            width: double.infinity,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const SizedBox(height: 30),
                GestureDetector(
                  onTap: _connectPrinter,
                  child: const Text('Connect printer'),
                ),
                const SizedBox(height: 30),
                GestureDetector(
                  onTap: _printInventoryBill,
                  child: const Text('Print inventory bill'),
                ),
                const SizedBox(height: 30),
                GestureDetector(
                  onTap: _printInventoryAdjustment,
                  child: const Text('Print inventory adjustment'),
                ),
                const SizedBox(height: 30),
                GestureDetector(
                  onTap: _printCheckoutBill,
                  child: const Text('Print checkout bill'),
                ),
                const SizedBox(height: 30),
                GestureDetector(
                  onTap: _printTransactionDetails,
                  child: const Text('Print transaction details'),
                ),
                const SizedBox(height: 30),
                GestureDetector(
                  onTap: _printShippingLabel,
                  child: const Text('Print shipping label'),
                ),

                const SizedBox(height: 30),
                const Text('-----以下是安卓測試-----'),
                Button("connect", () {
                  TP809.connectPrinter(ipAddress: "192.168.31.64", port: "9100").then((value) => print("connect success")).catchError((e) {
                    print(e.toString());
                  });
                }),
                Button("disconnect", () {
                  TP809.disconnectPrinter().then((value) => print("disconnect success")).catchError((e) {
                    print(e.toString());
                  });
                }),
                Button("print Text", () {
                  TP809.printText(text: content).then((value) => print("print text success")).catchError((e) {
                    print(e.toString());
                  });
                }),
                Button("print Image", printImage)
              ],
            ),
          ),
        )
      ),
    );
  }
}

class Button extends StatelessWidget {
  String text;
  Function() onTap;

  Button(this.text, this.onTap);

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: onTap, child: Container(height: 48, width: 120, alignment: Alignment.center, color: Colors.lime, child: Text("$text")));
  }
}
